//
//  XBAttributeLabel.m
//  AttributedStringTest
//
//  Created by xp on 17/4/18.
//  Copyright © 2017年 sun huayu. All rights reserved.
//

#import "XBAttributeLabel.h"
@interface XBAttributeLabel()
{
    
}
@property (nonatomic,retain)NSMutableAttributedString *attributeStr;
@end
@implementation XBAttributeLabel
-(void)setText:(NSString *)text
{
    [super setText:text];
    if (text == nil) {
        self.attributeStr = nil;
    }else{
        self.attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
    }

}
// 设置某段字的颜色
- (void)setAttributeColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length{
    if (location < 0||location>self.text.length-1||location+length>self.text.length) {
        return;
    }
    [_attributeStr addAttribute:NSForegroundColorAttributeName
                       value:color
                       range:NSMakeRange(location, length)];
    self.attributedText=_attributeStr;
}

// 设置某段字的字体
- (void)setAttributeFont:(UIFont *)font fromIndex:(NSInteger)location length:(NSInteger)length{
    if (location < 0||location>self.text.length-1||length+location>self.text.length) {
        return;
    }
    [_attributeStr addAttribute:NSFontAttributeName
                       value:font
                       range:NSMakeRange(location, length)];
     self.attributedText=_attributeStr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
